/*  Init
 *
 *  This routine is the initialization task for this test program.
 *  It is called from init_exec and has the responsibility for creating
 *  and starting the tasks that make up the test.  If the time of day
 *  clock is required for the test, it should also be set to a known
 *  value by this function.
 *
 *  Input parameters:  NONE
 *
 *  Output parameters:  NONE
 *
 *  COPYRIGHT (c) 1989-1999.
 *  On-Line Applications Research Corporation (OAR).
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.com/license/LICENSE.
 *
 *   $Id: init.c,v 0.01 2009/08/25 roxana $ 
 */

#define CONFIGURE_INIT
#include "system.h"
#include <stdio.h>
#include <stdlib.h>
#include <rtems/fb.h>
#include <fcntl.h>

rtems_task Init( rtems_task_argument ignored )
{

	static int fb =0;		/* Framebuffer file handle. */
	char *	env = 0;
	uint32_t *  fbp = 0;
	struct fb_var_screeninfo fb_var;
	struct fb_fix_screeninfo fb_fix;
	int x = 0, y = 0;
        int location = 0;
	char buff[65536]; //size is fb_fix.smem_len, the maximum number of bytes that can be read/wrote

	/* locate and open framebuffer, get info*/
	if(!(env = getenv("FRAMEBUFFER")))
		env = "/dev/fb0";
	fb = open(env, O_RDWR);

	if(fb < 0) {
		perror("Error: cannot open framebuffer device.\n");
		goto fail;
	}

	//printf("fb1 =%d \n",fb);

	if( ioctl( fb, FBIOGET_FSCREENINFO, &fb_fix ) || ioctl( fb, FBIOGET_VSCREENINFO, &fb_var ) )
	{
	        perror("Error: getting screen info\n" );
		exit(1);
	}

	printf("%dx%d, %dbpp\n", fb_var.xres, fb_var.yres, fb_var.bits_per_pixel);

	// Map the device to memory
	fbp = (char *) fb_fix.smem_start;


	if ((int)fbp < 0) {
	        perror("Error mapping FB memory to user space\n" );
		goto fail;
	}

		
	/* clear the framebuffer */
	memset((void *) fb_fix.smem_start,0xff, fb_fix.smem_len);


	/* TEST the OPEN case */

	if ( open(env, O_RDWR) < 0){
		printf("framebuffer is already open\n");
	}
	
	/* TEST READ/WRITE cases */ 
	
	/* 1 lseek = 0 --- at the begining of the memory*/
	printf("\nTHE CURSOR IS SET AT %d AND THE MUXIMUM NUMBER OF BYTES THAT CAN BE READ IS: %d\n", lseek(fb,0, SEEK_SET),fb_fix.smem_len - lseek(fb,0, SEEK_SET));
	printf("----------------------------------------------------------------\n");

	lseek(fb,0, SEEK_SET);
	printf("Reading a negative number of bytes, read %d, memory %d\n",read(fb, buff, -3), fb_fix.smem_len);
	lseek(fb,0, SEEK_SET);
	printf("Writing a negative number of bytes, wrote %d, memory %d\n",write(fb, buff, -3), fb_fix.smem_len);
	lseek(fb,0, SEEK_SET);
	printf("Reading 0 bytes,read %d\n", read(fb, buff, 0));
	lseek(fb,0, SEEK_SET);
	printf("Writing 0 bytes, wrote %d\n", write(fb, buff, 0));
	lseek(fb,0, SEEK_SET);
        printf("Reading 10 bytes, read %d\n", read(fb, buff, 10));
	lseek(fb,0, SEEK_SET);
        printf("Writing 10 bytes, wrote %d\n", write(fb, buff, 10));
	lseek(fb,0, SEEK_SET);
	printf("Reading %d bytes, read %d\n", fb_fix.smem_len, read(fb, buff, fb_fix.smem_len)); 
	lseek(fb,0, SEEK_SET);
	printf("Writing %d bytes, read %d\n", fb_fix.smem_len, read(fb, buff, fb_fix.smem_len)); 
	lseek(fb,0, SEEK_SET);
	printf("Reading %d bytes, read %d form %d bytes that fb has\n", fb_fix.smem_len + 10, read(fb, buff, fb_fix.smem_len + 10), fb_fix.smem_len ); 
	lseek(fb,0, SEEK_SET);
	printf("Writing %d bytes, read %d form %d bytes that fb has\n", fb_fix.smem_len + 10, read(fb, buff, fb_fix.smem_len + 10), fb_fix.smem_len ); 

	
	/* 2 lseek = len/2 --- in the middle of fb memory */
	printf("\nTHE CURSOR IS SET AT %d AND THE MUXIMUM NUMBER OF BYTES THAT CAN BE READ IS: %d\n", lseek(fb,fb_fix.smem_len/2, SEEK_SET),fb_fix.smem_len - lseek(fb,fb_fix.smem_len/2, SEEK_SET));
	printf("----------------------------------------------------------------\n");

	lseek(fb,fb_fix.smem_len/2, SEEK_SET);
	printf("Reading a negative number of bytes, read %d, memory %d\n",read(fb, buff, -3), fb_fix.smem_len);
	lseek(fb,fb_fix.smem_len/2, SEEK_SET);
	printf("Writing a negative number of bytes, wrote %d, memory %d\n",write(fb, buff, -3), fb_fix.smem_len);
	lseek(fb,fb_fix.smem_len/2, SEEK_SET);
	printf("Reading 0 bytes,read %d\n", read(fb, buff, 0));
	lseek(fb,fb_fix.smem_len/2, SEEK_SET);
	printf("Writing 0 bytes, wrote %d\n", write(fb, buff, 0));
	lseek(fb,fb_fix.smem_len/2, SEEK_SET);
        printf("Reading 10 bytes, read %d\n", read(fb, buff, 10));
	lseek(fb,fb_fix.smem_len/2, SEEK_SET);
        printf("Writing 10 bytes, wrote %d\n", write(fb, buff, 10));
	lseek(fb,fb_fix.smem_len/2, SEEK_SET);
	printf("Reading %d bytes, read %d\n", fb_fix.smem_len/2, read(fb, buff, fb_fix.smem_len/2)); 
	lseek(fb,fb_fix.smem_len/2, SEEK_SET);
	printf("Writing %d bytes, read %d\n", fb_fix.smem_len/2, read(fb, buff, fb_fix.smem_len/2)); 
	lseek(fb,fb_fix.smem_len/2, SEEK_SET);
	printf("Reading %d bytes, read %d form %d bytes that fb has\n", fb_fix.smem_len/2 + 10, read(fb, buff, fb_fix.smem_len/2 + 10), fb_fix.smem_len ); 
	lseek(fb,fb_fix.smem_len/2, SEEK_SET);
	printf("Writing  %d bytes, read %d form %d bytes that fb has\n", fb_fix.smem_len/2 + 10, read(fb, buff, fb_fix.smem_len/2 + 10), fb_fix.smem_len );

	/* 3 lseek = len --- at the end of fb memory */
	printf("\nTHE CURSOR IS SET AT %d AND THE MUXIMUM NUMBER OF BYTES THAT CAN BE READ IS: %d\n", lseek(fb,fb_fix.smem_len, SEEK_SET),fb_fix.smem_len - lseek(fb,fb_fix.smem_len, SEEK_SET));
	printf("----------------------------------------------------------------\n");

	lseek(fb,fb_fix.smem_len, SEEK_SET);
	printf("Reading a negative number of bytes, read %d, memory %d\n",read(fb, buff, -3), fb_fix.smem_len);
	lseek(fb,fb_fix.smem_len, SEEK_SET);
	printf("Writing a negative number of bytes, wrote %d, memory %d\n",write(fb, buff, -3), fb_fix.smem_len);
	lseek(fb,fb_fix.smem_len, SEEK_SET);
	printf("Reading 0 bytes,read %d\n", read(fb, buff, 0));
	lseek(fb,fb_fix.smem_len, SEEK_SET);
	printf("Writing 0 bytes, wrote %d\n", write(fb, buff, 0));
	lseek(fb,fb_fix.smem_len, SEEK_SET);
        printf("Reading 10 bytes, read %d\n", read(fb, buff, 10));
	lseek(fb,fb_fix.smem_len, SEEK_SET);
        printf("Writing 10 bytes, wrote %d\n", write(fb, buff, 10));

	exit(0);
  	fail:
	close(fb);
}
